package com.scwatch.core.fulltext;

import com.scwatch.common.fulltext.LuceneIndexTemplate;
import com.scwatch.common.orm.Limitable;
import com.scwatch.core.domain.Special;
import com.scwatch.core.service.SpecialService;
import com.scwatch.core.service.TaskService;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * SpecialFulltextServiceImpl
 * 
 * @author gjl
 * 
 */
@Service
@Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
public class SpecialFulltextServiceImpl implements SpecialFulltextService {
	public List<Special> list(Integer[] siteIds,Integer[] excludeId,Integer[] includeId,String title,
			Integer fragmentSize, Limitable limitable,Sort sort) {
		Query query = FSpecial.query(analyzer, siteIds,excludeId,includeId,title);;
		List<String> idList = template.list(query, FNode.ID, limitable, sort);
		List<Integer> ids = FSpecial.idsFromString(idList);
		List<Special> list;
		if (!ids.isEmpty()) {
			List<Special> temp = service.findAll(ids);
			list = new ArrayList<Special>(temp.size());
			for (int i = 0, len = ids.size(); i < len; i++) {
				for (Special special : temp) {
					if (special.getId().equals(ids.get(i))) {
						list.add(special);
						break;
					}
				}
			}
		} else {
			list = Collections.emptyList();
		}

		return list;
	}

	public Page<Special> page(Integer[] siteIds,Integer[] excludeId,Integer[] includeId,String title,
                           Integer fragmentSize,Pageable pageable, Sort sort) {
		Query query = FSpecial.query(analyzer, siteIds,excludeId,includeId,title);
		Page<String> idPage = template.page(query, FSpecial.ID, pageable, sort);
		List<Integer> ids = FSpecial.idsFromString(idPage.getContent());
		List<Special> content;
		if (!ids.isEmpty()) {
			List<Special> temp = service.findAll(ids);
			content = new ArrayList<Special>(temp.size());
			for (int i = 0, len = ids.size(); i < len; i++) {
				for (Special special : temp) {
					if (special.getId().equals(ids.get(i))) {
						content.add(special);
						break;
					}
				}
			}
		} else {
			content = Collections.emptyList();
		}

		return new PageImpl<Special>(content, pageable, idPage.getTotalElements());
	}


	public void addDocument(Integer specId) {
		Special bean = service.get(specId);
		if(bean!=null){
            template.addDocument(FSpecial.doc(bean));
        }
	}

	public void updateDocument(Integer specId) {
        Special bean = service.get(specId);
		template.updateDocument(FSpecial.id(specId), FSpecial.doc(bean));
	}

	@Transactional
	public int addDocument(Integer siteId,TaskService taskService,Integer taskId) {
		Integer[] siteIds = new Integer[] { siteId };
		Query query = FSpecial.query(analyzer,siteIds,null,null,null);
		template.deleteDocuments(query);
		return dao.addDocument(siteId,taskService, taskId);
	}

	private Analyzer analyzer;
	private LuceneIndexTemplate template;
	private SpecialService service;
	private SpecialFulltextDao dao;

	@Autowired
	public void setAnalyzer(Analyzer analyzer) {
		this.analyzer = analyzer;
	}

	@Autowired
	public void setTemplate(LuceneIndexTemplate template) {
		this.template = template;
	}

    @Autowired
    public void setService(SpecialService service) {
        this.service = service;
    }

	@Autowired
	public void setDao(SpecialFulltextDao dao) {
		this.dao = dao;
	}
}
